﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Election.DeclareWarDecision
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.BarterSystem.Barterables;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Election
{
  public class DeclareWarDecision : KingdomDecision
  {
    [SaveableField(101)]
    public readonly IFaction FactionToDeclareWarOn;

    internal static void AutoGeneratedStaticCollectObjectsDeclareWarDecision(
      object o,
      List<object> collectedObjects)
    {
      ((KingdomDecision) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.FactionToDeclareWarOn);
    }

    internal static object AutoGeneratedGetMemberValueFactionToDeclareWarOn(object o)
    {
      return (object) ((DeclareWarDecision) o).FactionToDeclareWarOn;
    }

    public DeclareWarDecision(Clan proposerClan, IFaction factionToDeclareWarOn)
      : base(proposerClan)
    {
      this.FactionToDeclareWarOn = factionToDeclareWarOn;
    }

    public override bool IsAllowed()
    {
      return !this.FactionToDeclareWarOn.IsKingdomFaction || Campaign.Current.Models.KingdomDecisionPermissionModel.IsWarDecisionAllowedBetweenKingdoms(this.Kingdom, this.FactionToDeclareWarOn as Kingdom, out TextObject _);
    }

    public override int GetProposalInfluenceCost()
    {
      return Campaign.Current.Models.DiplomacyModel.GetInfluenceCostOfProposingWar(this.ProposerClan);
    }

    public override TextObject GetGeneralTitle()
    {
      TextObject generalTitle = new TextObject("{=rtfoywJl}Declare war on the {KINGDOM_NAME}");
      generalTitle.SetTextVariable("KINGDOM_NAME", this.FactionToDeclareWarOn.InformalName);
      return generalTitle;
    }

    public override TextObject GetSupportTitle()
    {
      TextObject supportTitle = new TextObject("{=xM97H0oR}Vote for declaring war on the {KINGDOM_NAME}.");
      supportTitle.SetTextVariable("KINGDOM_NAME", this.FactionToDeclareWarOn.InformalName);
      return supportTitle;
    }

    public override TextObject GetChooseTitle()
    {
      TextObject chooseTitle = new TextObject("{=aQAI99d4}Declaring War on the {KINGDOM_NAME}");
      chooseTitle.SetTextVariable("KINGDOM_NAME", this.FactionToDeclareWarOn.InformalName);
      return chooseTitle;
    }

    public override TextObject GetSupportDescription()
    {
      TextObject supportDescription = new TextObject("{=KSrNutEO}{FACTION_LEADER} will decide if war will be declared on the {KINGDOM_NAME}. You can pick your stance regarding this decision.");
      supportDescription.SetTextVariable("FACTION_LEADER", this.DetermineChooser().Leader.Name);
      supportDescription.SetTextVariable("KINGDOM_NAME", this.FactionToDeclareWarOn.InformalName);
      return supportDescription;
    }

    public override TextObject GetChooseDescription()
    {
      TextObject chooseDescription = new TextObject("{=4JSzHkpt}As {?IS_FEMALE}queen{?}king{\\?}, you must decide if war will be declared on the {KINGDOM_NAME}");
      chooseDescription.SetTextVariable("IS_FEMALE", this.DetermineChooser().Leader.IsFemale ? 1 : 0);
      chooseDescription.SetTextVariable("KINGDOM_NAME", this.FactionToDeclareWarOn.InformalName);
      return chooseDescription;
    }

    public override IEnumerable<DecisionOutcome> DetermineInitialCandidates()
    {
      DeclareWarDecision declareWarDecision = this;
      yield return (DecisionOutcome) new DeclareWarDecision.DeclareWarDecisionOutcome(true, declareWarDecision.Kingdom, declareWarDecision.FactionToDeclareWarOn);
      yield return (DecisionOutcome) new DeclareWarDecision.DeclareWarDecisionOutcome(false, declareWarDecision.Kingdom, declareWarDecision.FactionToDeclareWarOn);
    }

    public override Clan DetermineChooser() => this.Kingdom.RulingClan;

    protected override bool ShouldBeCancelledInternal()
    {
      return this.Kingdom.IsAtWarWith(this.FactionToDeclareWarOn);
    }

    public override void DetermineSponsors(MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      foreach (DecisionOutcome possibleOutcome in (List<DecisionOutcome>) possibleOutcomes)
      {
        if (((DeclareWarDecision.DeclareWarDecisionOutcome) possibleOutcome).ShouldWarBeDeclared)
          possibleOutcome.SetSponsor(this.ProposerClan);
        else
          this.AssignDefaultSponsor(possibleOutcome);
      }
    }

    public override void ApplyChosenOutcome(DecisionOutcome chosenOutcome)
    {
      if (!((DeclareWarDecision.DeclareWarDecisionOutcome) chosenOutcome).ShouldWarBeDeclared)
        return;
      DeclareWarAction.ApplyByKingdomDecision((IFaction) this.Kingdom, this.FactionToDeclareWarOn);
    }

    public override TextObject GetSecondaryEffects()
    {
      return new TextObject("{=!}All supporters gains some relation with each other.");
    }

    public override void ApplySecondaryEffects(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes,
      DecisionOutcome chosenOutcome)
    {
    }

    public override TextObject GetChosenOutcomeText(
      DecisionOutcome chosenOutcome,
      KingdomDecision.SupportStatus supportStatus,
      bool isShortVersion = false)
    {
      TextObject parent;
      if (((DeclareWarDecision.DeclareWarDecisionOutcome) chosenOutcome).ShouldWarBeDeclared)
      {
        if (this.IsSingleClanDecision())
        {
          parent = new TextObject("{=Shk9QCDg}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} has declared war on the {KINGDOM}.");
        }
        else
        {
          switch (supportStatus)
          {
            case KingdomDecision.SupportStatus.Majority:
              parent = new TextObject("{=cRpsjvOM}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} has declared war on the {KINGDOM} with the support of {?AGGRESSOR_RULER.GENDER}her{?}his{\\?} council.");
              break;
            case KingdomDecision.SupportStatus.Minority:
              parent = new TextObject("{=DphlPaF9}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} has declared war on the {KINGDOM} despite opposition from {?AGGRESSOR_RULER.GENDER}her{?}his{\\?} council.");
              break;
            default:
              parent = new TextObject("{=RH8mgLEk}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} has declared war on the {KINGDOM} with support from half of {?AGGRESSOR_RULER.GENDER}her{?}his{\\?} council.");
              break;
          }
        }
      }
      else if (this.IsSingleClanDecision())
      {
        parent = new TextObject("{=9WDK03GW}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} chose not to go to war with the {KINGDOM}.");
      }
      else
      {
        switch (supportStatus)
        {
          case KingdomDecision.SupportStatus.Majority:
            parent = new TextObject("{=8RBZaQKk}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} chose not to go to war with the {KINGDOM} with the support of {?AGGRESSOR_RULER.GENDER}her{?}his{\\?} council.");
            break;
          case KingdomDecision.SupportStatus.Minority:
            parent = new TextObject("{=XaZyU3Cn}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} chose not to go to war with the {KINGDOM} despite the wishes of {?AGGRESSOR_RULER.GENDER}her{?}his{\\?} council.");
            break;
          default:
            parent = new TextObject("{=5L0wDGhR}{AGGRESSOR_RULER.NAME} of the {AGGRESSOR_KINGDOM} chose not to go to war with the {KINGDOM}, with {?AGGRESSOR_RULER.GENDER}her{?}his{\\?} council evenly split.");
            break;
        }
      }
      StringHelpers.SetCharacterProperties("AGGRESSOR_RULER", this.Kingdom.Leader.CharacterObject, parent);
      parent.SetTextVariable("AGGRESSOR_KINGDOM", this.Kingdom.EncyclopediaLinkWithName);
      parent.SetTextVariable("KINGDOM", this.FactionToDeclareWarOn.InformalName);
      return parent;
    }

    public override DecisionOutcome GetQueriedDecisionOutcome(
      MBReadOnlyList<DecisionOutcome> possibleOutcomes)
    {
      return possibleOutcomes.FirstOrDefault<DecisionOutcome>((Func<DecisionOutcome, bool>) (t => ((DeclareWarDecision.DeclareWarDecisionOutcome) t).ShouldWarBeDeclared));
    }

    public float CalculateSupport(Clan clan)
    {
      return this.DetermineSupport(clan, (DecisionOutcome) new DeclareWarDecision.DeclareWarDecisionOutcome(true, this.Kingdom, this.FactionToDeclareWarOn));
    }

    public override float DetermineSupport(Clan clan, DecisionOutcome possibleOutcome)
    {
      Hero leader = clan.Leader;
      DeclareWarDecision.DeclareWarDecisionOutcome warDecisionOutcome = (DeclareWarDecision.DeclareWarDecisionOutcome) possibleOutcome;
      float num = (float) new DeclareWarBarterable((IFaction) this.Kingdom, this.FactionToDeclareWarOn).GetValueForFaction((IFaction) clan) * Campaign.Current.Models.DiplomacyModel.DenarsToInfluence();
      return warDecisionOutcome.ShouldWarBeDeclared ? num + (float) (clan.Leader.GetTraitLevel(DefaultTraits.Valor) * 20 - clan.Leader.GetTraitLevel(DefaultTraits.Mercy) * 10) : -num + (float) (-clan.Leader.GetTraitLevel(DefaultTraits.Valor) * 20 + clan.Leader.GetTraitLevel(DefaultTraits.Mercy) * 10);
    }

    public class DeclareWarDecisionOutcome : DecisionOutcome
    {
      [SaveableField(100)]
      public readonly bool ShouldWarBeDeclared;
      [SaveableField(110)]
      public readonly Kingdom Kingdom;
      [SaveableField(120)]
      public readonly IFaction FactionToDeclareWarOn;

      internal static void AutoGeneratedStaticCollectObjectsDeclareWarDecisionOutcome(
        object o,
        List<object> collectedObjects)
      {
        ((DecisionOutcome) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
      }

      protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
      {
        base.AutoGeneratedInstanceCollectObjects(collectedObjects);
        collectedObjects.Add((object) this.Kingdom);
        collectedObjects.Add((object) this.FactionToDeclareWarOn);
      }

      internal static object AutoGeneratedGetMemberValueShouldWarBeDeclared(object o)
      {
        return (object) ((DeclareWarDecision.DeclareWarDecisionOutcome) o).ShouldWarBeDeclared;
      }

      internal static object AutoGeneratedGetMemberValueKingdom(object o)
      {
        return (object) ((DeclareWarDecision.DeclareWarDecisionOutcome) o).Kingdom;
      }

      internal static object AutoGeneratedGetMemberValueFactionToDeclareWarOn(object o)
      {
        return (object) ((DeclareWarDecision.DeclareWarDecisionOutcome) o).FactionToDeclareWarOn;
      }

      public DeclareWarDecisionOutcome(
        bool shouldWarBeDeclared,
        Kingdom kingdom,
        IFaction factionToDeclareWarOn)
      {
        this.ShouldWarBeDeclared = shouldWarBeDeclared;
        this.Kingdom = kingdom;
        this.FactionToDeclareWarOn = factionToDeclareWarOn;
      }

      public override TextObject GetDecisionTitle()
      {
        TextObject decisionTitle = new TextObject("{=kakxnaN5}{?SUPPORT}Yes{?}No{\\?}");
        decisionTitle.SetTextVariable("SUPPORT", this.ShouldWarBeDeclared ? 1 : 0);
        return decisionTitle;
      }

      public override TextObject GetDecisionDescription()
      {
        if (this.SponsorClan != null && this.Kingdom != null && this.FactionToDeclareWarOn != null && this.SponsorClan != Clan.PlayerClan)
        {
          TextObject reason = TextObject.Empty;
          if (this.ShouldWarBeDeclared)
          {
            double scoreOfDeclaringWar = (double) Campaign.Current.Models.DiplomacyModel.GetScoreOfDeclaringWar((IFaction) this.Kingdom, this.FactionToDeclareWarOn, (IFaction) this.SponsorClan, out reason);
          }
          if (reason != TextObject.Empty)
            return reason;
        }
        return this.ShouldWarBeDeclared ? new TextObject("{=w9olmhv0}It is time to declare war") : new TextObject("{=epnk9qIt}We oppose a declaration of war");
      }

      public override string GetDecisionLink() => (string) null;

      public override ImageIdentifier GetDecisionImageIdentifier() => (ImageIdentifier) null;
    }
  }
}
